﻿/*-----------------------------------------------------------------------------------------------
// Copyright (C) 2016 华跃博弈科技有限公司 
//
// 文件名：        SysUserRepository.cs
// 文件功能描述：   用户信息数据持久化类
//
// 
// 创建标识：       庞永飞  2016-04-24
//
// 修改标识：
// 修改描述：
//
// 备注：	
// 				
//-----------------------------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using HYBY.Task.DataEntity;
using HYBY.Task.Repository.IRepository;

namespace HYBY.Task.Repository {
    
    public class SysUserRepository : BaseRepository<SysUserEntity>, ISysUserRepository {

        #region 常量

        private const String CONST_GetSysUser = "MAT_PLAT#GetSysUser";
        private const String CONST_GetSysUsers = "MAT_PLAT#GetSysUsers";
        private const String CONST_GetSysUserPaged = "MAT_PLAT#GetSysUserPaged";
        private const String CONST_GetSysUserRecordCount = "MAT_PLAT#GetSysUserRecordCount";
        private const String CONST_SaveSysUser = "MAT_PLAT#SaveSysUser";
        private const String CONST_DeleteSysUser = "MAT_PLAT#DeleteSysUser";

        #endregion

        #region 构造器

        public SysUserRepository() {

        }

        #endregion

        #region 公共方法


        /// <summary>
        /// 返回用户信息
        /// </summary>
        /// <param name="Id">用户编码</param>
        /// <returns></returns>
        public override SysUserEntity Get(object Id) {
            return Query<SysUserEntity>(CONST_GetSysUser, new { UserID = Id }).ToList().FirstOrDefault();
        }

        /// <summary>
        /// 返回所有用户信息
        /// </summary>
        /// <returns></returns>
        public List<SysUserEntity> GetSysUsers() {
            return Query<SysUserEntity>(CONST_GetSysUsers).ToList();
        }


        /// <summary>
        /// 分页方式返回用户信息
        /// </summary>
        /// <param name="PageIndex">页面索引</param>
        /// <param name="PageSize">页面大小</param>
        /// <param name="RecordCount">总记录数</param>
        /// <returns></returns>
        public List<SysUserEntity> GetSysUsers(String searchKey,int pageIndex, int pageSize, out int recordCount) {
            //设置Velocity 模板引擎参数值
            Dictionary<String, object> TemplateParams = new Dictionary<string, object>{
            {"UserName", String.IsNullOrEmpty(searchKey)?String.Empty:searchKey}
            };
            recordCount = Query<Int32>(CONST_GetSysUserRecordCount, TemplateParams,
                new { UserName = searchKey }).FirstOrDefault();

            return Query<SysUserEntity>(CONST_GetSysUserPaged, TemplateParams,
                new { PageIndex = pageIndex, PageSize = pageSize, UserName=searchKey }).ToList();
        }

        /// <summary>
        /// 保存用户信息领域实体到持久化存储
        /// </summary>
        /// <param name="entity">领域实体</param>    
        /// <returns></returns>
        public override bool Save(SysUserEntity entity) {
            return Execute(CONST_SaveSysUser, entity) > 0;
        }

        /// <summary>
        /// 从持久化存储中删除用户信息
        /// </summary>
        /// <param name="entity">领域实体</param>    
        /// <returns></returns>
        public override bool Delete(SysUserEntity entity) {
            return Execute(CONST_DeleteSysUser, new { UserID = entity.UserID }) > 0;
        }

        #endregion
    }
}

